package cn.com.thread;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;

public class MainTest {


    /**
     * 两个线程不是同一个对象，他们之间的内容互不共享
     */
    @Test
    public void targetDemo2Test(){
        Thread t1 = new Thread("t1"){
            @Override
            public void run() {
                //获取线程名称
                String threadName = Thread.currentThread().getName();
                for (int i = 0; i < 10; i++) {
                    System.out.println(threadName + "：Hello world!");
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread t2 = new Thread("t2"){
            @Override
            public void run() {
                //获取线程名称
                String threadName = Thread.currentThread().getName();
                for (int i = 0; i < 10; i++) {
                    System.out.println(threadName + "：Hello world!");
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        t1.start();
        t2.start();
    }

    /**
     * 虽然是两个线程，但是他们属于同一个实例对象，共享同一个对象内的所有成员
     */
    @Test
    public void targetDemo1Test(){
        TargetDemo1 targetDemo = new TargetDemo1();
        targetDemo.setList(new ArrayList<String>(Arrays.asList("1","2","3")));
        Thread t1 = new Thread(targetDemo, "t1");
        Thread t2 = new Thread(targetDemo, "t2");
        t1.start();
        t2.start();
    }
}
